

#### Do Formal Loans Boost SME Performance? Key Takeaways from a Meta-Analysis ####

#### Main R Script #####
rm(list = ls()) # Remove all objects from the environment
options(scipen = 999) 

# This line is to start the timer
start_time <- Sys.time() #Start time

# Load libraries ----
# Install the required packages
# renv::init()
# 
# required_packages <- c("tidyverse", "meta", "readxl", "ggplot2", "tidyr", "writexl", "baggr", "stargazer","rstan","openxlsx","forestplot", "ggpubr","xtable")
# 
# # Install any package that is not already installed
# for (pkg in required_packages) {
#   if (!requireNamespace(pkg, quietly = TRUE)) {
#     install.packages(pkg)
#   }
# }
# renv::snapshot()

# Load the libraries
library(tidyverse)   # For data manipulation and visualization (includes dplyr, ggplot2, tidyr, etc.)
library(meta)        # For conducting meta-analyses
library(readxl)      # For reading Excel files (.xlsx)
library(ggplot2)     # For creating plots and visualizations
library(tidyr)       # For reshaping and tidying data
library(writexl)     # For writing data frames to Excel files (.xlsx)
library(baggr)       # For Bayesian meta-analysis
library(stargazer)   # For creating formatted tables (e.g., regression output) for LaTeX, HTML, or text
library(rstan)       # For Bayesian modeling using Stan
library(openxlsx)    # For reading, writing, and editing Excel files without Java dependency
library(forestplot)  # For forest plot
library(ggpubr)      # For ggarrange plots   
library(xtable)      # For creating latex tables from dataframes

# Set package path ----

# This is the root directory where files are stored.
# For replication purposes, please replace this path with the location where you saved the "Reproducibility Package" folder.
path <- "C:/WBG/github/365v2/OneDrive_2025-06-23/Reproducibility Package/"

# Run the R scripts ----
# Source the scripts that are part of your project.

source(file.path(path, "Code/01-processing-data.R"))
source(file.path(path, "Code/02.1-Data Analysis -Employment.R"),  echo = TRUE)
source(file.path(path, "Code/02.2 Data Analysis -Sales.R"),  echo = TRUE)
source(file.path(path, "Code/02.3 Data Analysis -Profit.R"),  echo = TRUE)

## This line is to end the timer
end_time <- Sys.time() # End time

# Calculate execution time in minutes and seconds
execution_time <- as.numeric(difftime(end_time, start_time, units = "secs"))
minutes <- floor(execution_time / 60)
seconds <- round(execution_time %% 60)

# Print execution time in minutes and seconds
print(paste("Execution time:", minutes, "minutes and", seconds, "seconds"))
